package com.ph.order.api.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cm.ph.shopping.facade.order.entity.PhOrderAddress;
import cm.ph.shopping.facade.order.request.MemberRequest;
import cm.ph.shopping.facade.order.request.OnLineOrderRequest;
import cm.ph.shopping.facade.order.request.OnLineOrderSendRequest;
import cm.ph.shopping.facade.order.request.OrderAddressDefRequest;
import cm.ph.shopping.facade.order.request.PhOrderOnlineRequest;
import cm.ph.shopping.facade.order.request.SupplyerNameRequest;
import cm.ph.shopping.facade.order.request.UpdateOrderRequest;
import cm.ph.shopping.facade.order.request.UserRequest;
import cm.ph.shopping.facade.order.request.WebOnLineOrderRequest;
import cm.ph.shopping.facade.order.request.WebOnLineRequest;
import cm.ph.shopping.facade.order.service.IOrderService;
import cm.ph.shopping.facade.order.vo.QueryOnLineOrderDetailVo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.transform.MoneyTransUtil;
import com.ph.shopping.facade.permission.vo.SessionRoleVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;

@Controller
@RequestMapping("/api/order")
public class OrderController extends BaseController {

	@Reference(version = "1.0.0")
	private IOrderService iOrderService;

	/**
	 * @Title: addOnLineOrder
	 * @Description: 生成线上订单
	 * @author WQiang
	 * @date 2017年3月15日 上午9:26:07
	 * @param orderOnline
	 * @return
	 */
	@RequestMapping("addonlineorder")
	@ResponseBody
	public Result addOnLineOrder(PhOrderOnlineRequest request) {
		try {
			return iOrderService.insertPhOrderOnline(request);
		} catch (Exception e) {
			logger.error("生成线上订单异常!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: addOrderAddress
	 * @Description:新增收货地址
	 * @author WQiang
	 * @date 2017年3月18日 下午2:12:04
	 * @param orderAddress
	 * @return
	 */
	@RequestMapping("addorderaddress")
	@ResponseBody
	public Result addOrderAddress(PhOrderAddress orderAddress) {
		try {
			return iOrderService.insertOrderAddress(orderAddress);
		} catch (Exception e) {
			logger.error("新增收货地址失败!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: delOrderAddress
	 * @Description: 删除收货地址
	 * @author WQiang
	 * @date 2017年3月18日 下午2:16:20
	 * @return
	 */
	@RequestMapping("delorderaddress")
	@ResponseBody
	public Result delOrderAddress(long addrId) {
		try {
			return iOrderService.deleteOrderAddress(addrId);
		} catch (Exception e) {
			logger.error("删除收货地址失败!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: updOrderAddress
	 * @Description: 更新收货地址
	 * @author WQiang
	 * @date 2017年3月18日 下午2:20:13
	 * @param orderAddress
	 * @return
	 */
	@RequestMapping("updorderaddress")
	@ResponseBody
	public Result updOrderAddress(PhOrderAddress orderAddress) {
		try {
			return iOrderService.updateOrderAddress(orderAddress);
		} catch (Exception e) {
			logger.error("更新收货地址失败!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: updOrderAddressDef
	 * @Description: 设置默认地址
	 * @author WQiang
	 * @date 2017年3月18日 下午2:24:52
	 * @param request
	 * @return
	 */
	@RequestMapping("updorderaddressdef")
	@ResponseBody
	public Result updOrderAddressDef(OrderAddressDefRequest request) {
		try {
			return iOrderService.updateOrderAddrDef(request.getMemberId(), request.getAddrId());
		} catch (Exception e) {
			logger.error("设置默认地址失败!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: getOrderAddressList
	 * @Description: 收货地址列表
	 * @author WQiang
	 * @date 2017年3月18日 下午2:26:09
	 * @param memberId
	 * @return
	 */
	@RequestMapping("getorderaddresslist")
	@ResponseBody
	public Result getOrderAddressList(MemberRequest request) {
		return iOrderService.queryOrderAddress(request.getMemberId());
	}

	/**
	 * 
	 * @Title: updateOnLineOrder
	 * @Description: 更新订单状态
	 * @author WQiang
	 * @date 2017年3月15日 上午9:35:42
	 * @param orderId
	 * @return
	 */
	@RequestMapping("updateonlineorder")
	@ResponseBody
	public Result updateOnLineOrder(UpdateOrderRequest request) {
		try {
			if (iOrderService.updatePhOrderOnline(request.getOrderNo(), request.getStatus()) != 1) {
				return getResult(ResultEnum.UPDATE_ORDER_EXCEPTION);
			}
			return getResult(ResultEnum.SUCCESS);
		} catch (Exception e) {
			logger.error("更新线上订单异常!");
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}

	/**
	 * 
	 * @Title: toOrderList
	 * @Description: 跳转到订单列表界面
	 * @author WQiang
	 * @date 2017年3月21日 上午10:34:38
	 * @return
	 */
	@RequestMapping(value = "/tolistorders", method = RequestMethod.GET)
	public String toOrderList() {
		return "order/orderlist";
	}

	/**
	 * 
	 * @Title: selectOnLineOrder
	 * @Description: 查询后台订单
	 * @author WQiang
	 * @date 2017年3月15日 下午1:53:36
	 * @return
	 */
	@RequestMapping("/listonlineorder")
	@ResponseBody
	public Result listOnLineOrder(UserRequest request,PageBean pageBean) {
		try {
			SessionUserVo userVo = getLoginUser();
			List<SessionRoleVo> roles = userVo.getSessionRoleVo();
			for(SessionRoleVo roleVo : roles) {
				//管理员
				if(roleVo.getRoleCode() == 1) {
					request.setRoleCode((int)roleVo.getRoleCode());
					break;
				}
				
				if(roleVo.getRoleCode() == 2) {//供应商呢
					request.setRoleCode((int)roleVo.getRoleCode());
					request.setSupplierId(userVo.getId());
				}
				
				//代理商
				if(roleVo.getRoleCode() == 3 || roleVo.getRoleCode() == 4 || roleVo.getRoleCode() == 5) {
					request.setRoleCode((int)roleVo.getRoleCode());
					request.setSupplierId(userVo.getId());
				}
			}
			
			return iOrderService.findOnlineOrders(request, pageBean);
		} catch (Exception e) {
			logger.error("线上订单查询异常!");
			e.printStackTrace();
			return getResult(ResultEnum.SELECT_ORDER_EXCEPTION);
		}
	}
	
	
	/**
	 * 
	* @Title: getOnLineOrderDetail
	* @Description: 获取订单详情
	* @author WQiang
	* @date  2017年3月31日 上午10:17:47
	* @param request
	* @return
	 */
	@RequestMapping("getorderdetail")
	public ModelAndView getOnLineOrderDetail(OnLineOrderRequest request) {
		
		try {
			ModelAndView mv = new ModelAndView("/order/orderDetail");
			QueryOnLineOrderDetailVo detailVo = iOrderService.getOrderDetail(request.getId());
			detailVo.setPrice(MoneyTransUtil.transDivisDouble(detailVo.getPrice1()));
			detailVo.setTotalMoney(MoneyTransUtil.transDivisDouble(detailVo.getPrice1()) * detailVo.getNum());
			detailVo.setPostage(MoneyTransUtil.transDivisDouble(detailVo.getPostage1()));
			detailVo.setPayMoney(MoneyTransUtil.transDivisDouble(detailVo.getPayMoney1()));
			
			mv.addObject("orderDetail", detailVo);
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @Title: listWebOnLineOrders
	 * @Description: 查询会员订单
	 * @author WQiang
	 * @date 2017年3月28日 下午4:39:03
	 * @param request
	 * @return
	 */
	@RequestMapping("listwebonlineorders")
	@ResponseBody
	public Result listWebOnLineOrders(WebOnLineOrderRequest request) {
		return iOrderService.queryWebOnLineOrders(request.getMemberId(), request.getStatus(),
				request.getPageNum(), request.getPageSize());
	}
	
	/**
	 * 
	* @Title: getWebOnLineOrderDetail
	* @Description: 获取会员订单详情
	* @author WQiang
	* @date  2017年3月28日 下午6:42:40
	* @param orderId
	* @return
	 */
	@RequestMapping("getwebonlineorderdetail")
	@ResponseBody
	public Result getWebOnLineOrderDetail(WebOnLineRequest request) throws Exception {
		return iOrderService.getWebOnLineOrderDetail(request.getOrderId(),request.getDeliveryType());
	}
	
	
	public Result getWebOnLineOrderDetail(long orderId ) {
		return iOrderService.getWebOnLineOrderDetail(orderId);
	}
	
	/**
	 * 
	* @Title: getSupplyerName
	* @Description: 获取供应商名称
	* @author WQiang
	* @date  2017年3月29日 下午2:36:38
	* @param request
	* @return
	 */
	@RequestMapping("getsupplyername")
	@ResponseBody
	public Result getSupplyerName(SupplyerNameRequest request) {
		return iOrderService.querySupplyerName(request.getProductId());
	}
	
	/**
	 * 
	* @Title: getUserAddress
	* @Description: 获取发货地址
	* @author WQiang
	* @date  2017年3月31日 下午4:42:22
	* @param request
	* @return
	 */
	@RequestMapping("getuseraddress")
	@ResponseBody
	public Result getUserAddress() {
		SessionUserVo userVo = getLoginUser();
		return iOrderService.getUserAddress(userVo.getId());
	}
	
	/**
	 * 
	* @Title: updateOrderSend
	* @Description: 会员订单发货
	* @author WQiang
	* @date  2017年3月31日 下午5:44:17
	* @return
	 */
	@RequestMapping("tosend")
	@ResponseBody
	public Result updateOrderSend(OnLineOrderSendRequest request) {
		try {
			return iOrderService.updateOrderToSend(request.getStatus(), request.getLogisticName(), request.getLogisticNo(), request.getOrderId());
		} catch (Exception e) {
			e.printStackTrace();
			return getResult(ResultEnum.INTERNAL_SERVER_ERROR);
		}
	}
}
